<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:insert="common/header :: head"></head>
<!-- 公告管理 -->
<style>
    .el-dialog  {resize: none;}
</style>
<body>
<div id="wrapper" v-cloak>
    <el-input size="small" v-model="search" style="width: 20%;" suffix-icon="el-icon-search" placeholder="请输入标题按回车搜索"
              @keyup.enter.native="loadTable"></el-input>
    <el-button size="small" @click="add" type="primary"  style="margin: 10px 0">新增</el-button>
    <div style="margin-top: -25px;">
        <el-divider></el-divider>
    </div>

    <el-table v-loading="loading" :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="50">
        </el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="content" label="内容"></el-table-column>
        <el-table-column prop="time" label="发布时间"></el-table-column>
        <el-table-column
                fixed="right"
                label="操作"
                width="150">
            <template slot-scope="scope">
                <el-button type="primary" @click="edit(scope.row)"  icon="el-icon-edit" circle ></el-button>
                <el-popconfirm
                        @onConfirm="del(scope.row.id)"
                        title="确定删除？">
                    <el-button type="danger" icon="el-icon-delete" circle slot="reference" ></el-button>
                </el-popconfirm>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="background-color: white">
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[5, 10, 20, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
    </div>

    <el-dialog title="公告信息" :visible.sync="dialogFormVisible" width="600px"
               :lock-scroll="true" :close-on-click-modal="false"
               :close-on-press-escape="false" >
        <el-form :model="entity">
            <el-form-item label="标题" label-width="120px">
                <el-input placeholder="请输入标题" v-model="entity.title" autocomplete="off" style="width: 80%"></el-input>
            </el-form-item>
            <el-form-item  label="内容" label-width="120px">
                <el-input placeholder="请输入内容" type="textarea" v-model="entity.content" :rows="5" autocomplete="off" style="width: 80%"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button size="small" type="primary" @click="save">确 定</el-button>
            <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        </div>
    </el-dialog>
</div>
<script type="text/javascript">
    let urlBase = "../api/notice";
    new Vue({
        el: "#wrapper",
        data: {
            user: {},
            tableData: [],
            pageNum: 1,
            pageSize: 10,
            total: 0,
            dialogFormVisible: false,
            entity: {},
            isCollapse: false,
            search: '',
            // 页面加载效果
            loading: false,
        },
        created() {
            this.user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {};
            this.loadTable();
        },
        methods: {
            handleCollapse() {
                this.isCollapse = !this.isCollapse;
            },
            logout() {
                $.get("../api/user/logout");
                sessionStorage.removeItem("user");
                location.href = "../login";
            },
            loadTable() {
                this.$data.loading=true;
                $.get(urlBase + "/page?pageNum=" + this.pageNum + "&pageSize=" + this.pageSize + "&name=" + this.search).then(res => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                })
                this.$data.loading=false;
            },
            exp() {
                window.open(urlBase + "/export");
            },
            handleSizeChange(pageSize) {
                this.pageSize = pageSize;
                this.loadTable();
            },
            handleCurrentChange(pageNum) {
                this.pageNum = pageNum;
                this.loadTable();
            },
            add() {
                this.entity = {};
                this.dialogFormVisible = true;
            },
            save() {
                let mUrl = this.entity.id ? "/update":"/save";
                $.ajax({
                    url: urlBase+mUrl,
                    type:"POST",
                    contentType: "application/json",
                    data: JSON.stringify(this.entity)
                }).then(res => {
                    if (res.code == 1) {
                        this.$message({message: "保存成功", type: "success"});
                        this.loadTable();
                    } else {
                        this.$message({message: res.msg, type: "error"})
                    }
                    this.dialogFormVisible = false;
                })
            },
            edit(obj) {
                this.entity = JSON.parse(JSON.stringify(obj));
                this.dialogFormVisible = true;
            },
            del(id) {
                $.ajax({
                    url: urlBase +"del/"+ id,
                    type: "GET"
                }).then(res => {
                    if (res.code == 1) {
                        this.$message({message: "删除成功", type: "success"})
                        this.loadTable();
                    } else {
                        this.$message({message: res.msg, type: "error"})
                    }
                })
            }
        }
    })
</script>
</body>
</html>
